package com.autumn.platform.dao.schema.parser;

import java.io.InputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.ibatis.parsing.XNode;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.EntityResolver;

import com.autumn.platform.dao.schema.SchemaXNode;

public class SchemaXPathParser extends org.apache.ibatis.parsing.XPathParser {

    private Properties variables;

    public SchemaXPathParser(Document document, boolean validation, Properties variables, EntityResolver entityResolver) {
        super(document, validation, variables, entityResolver);
        this.variables = variables;
    }

    public SchemaXPathParser(Document document, boolean validation, Properties variables) {
        super(document, validation, variables);
        this.variables = variables;
    }

    public SchemaXPathParser(Document document, boolean validation) {
        super(document, validation);
    }

    public SchemaXPathParser(Document document) {
        super(document);
    }

    public SchemaXPathParser(InputStream inputStream, boolean validation, Properties variables, EntityResolver entityResolver) {
        super(inputStream, validation, variables, entityResolver);
        this.variables = variables;

    }

    public SchemaXPathParser(InputStream inputStream, boolean validation, Properties variables) {
        super(inputStream, validation, variables);
        this.variables = variables;

    }

    public SchemaXPathParser(InputStream inputStream, boolean validation) {
        super(inputStream, validation);

    }

    public SchemaXPathParser(InputStream inputStream) {
        super(inputStream);

    }

    public SchemaXPathParser(Reader reader, boolean validation, Properties variables, EntityResolver entityResolver) {
        super(reader, validation, variables, entityResolver);
        this.variables = variables;
    }

    public SchemaXPathParser(Reader reader, boolean validation, Properties variables) {
        super(reader, validation, variables);
        this.variables = variables;
    }

    public SchemaXPathParser(Reader reader, boolean validation) {
        super(reader, validation);
    }

    public SchemaXPathParser(Reader reader) {
        super(reader);
    }

    public SchemaXPathParser(String xml, boolean validation, Properties variables, EntityResolver entityResolver) {
        super(xml, validation, variables, entityResolver);
        this.variables = variables;
    }

    public SchemaXPathParser(String xml, boolean validation, Properties variables) {
        super(xml, validation, variables);
        this.variables = variables;
    }

    public SchemaXPathParser(String xml, boolean validation) {
        super(xml, validation);
    }

    public SchemaXPathParser(String xml) {
        super(xml);
    }

    @Override
    public List<XNode> evalNodes(Object root, String expression) {
        if ("selectKey".equals(expression) && root instanceof Node) {//原生写法，这里获取不到selectKey子节点
            Node node = (Node) root;
            List<XNode> xnodes = new ArrayList<XNode>();
            NodeList nodes = node.getChildNodes();
            for (int i = 0; i < nodes.getLength(); i++) {
                Node item = nodes.item(i);
                if (item instanceof Element && "selectKey".equals(item.getNodeName())) {
                    xnodes.add(new SchemaXNode(this, item, variables));
                }
            }
            return xnodes;
        } else {
            return super.evalNodes(root, expression);
        }
    }
}
